<!-- 自定义标准图表 设置百分百宽度 通过平台组件来适配尺寸 可以替换数据以及设置高级属性自定义扩展 详细查看组件示例.txt
  注:如需设置图片存放文件夹名称需应与当前文件名一致(1并在aircim.webpack.build.component.config.js配置需要打包的文件 2.npm run lib_c 执行打包命令  最后的组件会打包在components/grids中)-->
<template>
  <div class="scale-box" :id="dynamicIdName()">
    <div
      class="business-situation-box animate__animated"
      :class="dynamicClassName"
      style="width: 460px;height: 632px;"
      :id="dynamicChildName()"
    >
      <h1 class="h1_title">
        <span>办理概况</span>
      </h1>
      <div class="business">
        <div class="business-item">
          <p>申报量</p>
          <p class="num"><em>120</em>件</p>
        </div>
        <div class="business-item">
          <p>受理量</p>
          <p class="num"><em>120</em>件</p>
        </div>
        <div class="business-item">
          <p>办结量</p>
          <p class="num"><em>120</em>件</p>
        </div>
      </div>
      <h2 class="h2_title">
        <span>近半年办件趋势</span>
      </h2>
      <div
        class="chart-wrap"
        style="height: 157px;margin-top: 18px;position: relative;"
      >
        <LineChart></LineChart>
      </div>

      <h2 class="h2_title">
        <span>办件分布</span>
        <p class="h2_tab">
          <span class="active">层级分布</span><span>类型分布</span>
        </p>
      </h2>
      <div class="pie-chart" style="height: 157px;position: relative;">
        <PieChart></PieChart>
      </div>
    </div>
  </div>
</template>
<script>
//引入封装方法
import encapsulation from "../../utils/aircimGird";
import LineChart from "../elements/LineChart.vue";
import PieChart from "../elements/PieChart.vue";
export default {
  //混入封装方法
  mixins: [encapsulation],
  components: {
    LineChart,
    PieChart,
  },
  data() {
    return {
      resizeObserver: null,
      //动态Id
      timestamp: null,
    };
  },
  mounted() {
    this.timestamp = new Date().getTime();
    this.$nextTick(() => {
      // 监听盒子变化
      this.listenChangeFn();
    });
  },
  methods: {
    //监听外部盒子的大小 然后对数据井行缩放
    scaleFn() {
      const parentBox = document.getElementById(`dynamic-${this.timestamp}`);
      const childBox = document.getElementById(
        `dynamicChild-${this.timestamp}`
      );
      const parentWidth = parentBox.offsetWidth;
      const parentHeight = parentBox.offsetHeight;
      const childWidth = childBox.offsetWidth;
      const childHeight = childBox.offsetHeight;
      let scaleX, scaleY;
      if (parentWidth < childWidth || parentHeight < childHeight) {
        const scaleBasedOnWidth = parentWidth / childWidth;
        const scaleBasedOnHeight = parentHeight / childHeight;
        scaleX = scaleBasedOnWidth;
        scaleY = scaleBasedOnHeight;
      } else {
        scaleX = parentWidth / childWidth;
        scaleY = parentHeight / childHeight;
      }
      childBox.style.transform = `scale(${scaleX}, ${scaleY})`;
    },
    // 监听盒子变化
    listenChangeFn() {
      let that = this;
      const element = document.getElementById(`dynamic-${this.timestamp}`);
      // console.log(element, "element");
      if (element) {
        new ResizeObserver(() => {
          // 处理元素大小变化的逻辑
          that.scaleFn();
        }).observe(element);
      }
    },
    // 绑定动画样式 animate__flash
    bindAnimationFn(animateName) {
      if (animateName != null) {
        this.dynamicClassName = animateName;
      }
    },
    //定义唯一ID名
    dynamicIdName() {
      return `dynamic-${this.timestamp}`;
    },
    //定义唯一ID名
    dynamicChildName() {
      return `dynamicChild-${this.timestamp}`;
    },
  },
  // 销毁监听
  beforeDestroy() {
    // 组件销毁前停止监听
    if (this.resizeObserver) {
      this.resizeObserver.disconnect();
    }
    if (this.requestTime != null) {
      clearInterval(this.requestTime);
    }
  },
};
</script>
<style lang="scss" scoped>
@import "../../static/assets/common.scss";
</style>
<style lang="scss" scoped>
.scale-box {
  width: 100%;
  height: 100%;
}

.business-situation-box {
  position: absolute;
  padding: 0 20px;
  box-sizing: border-box;
  overflow: hidden;
  transform-origin: left top;
  background-image: url(../../../thumbnail/backImg/new-border-green.png);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  color: #fff;
}
.business {
  margin-top: 17px;
  display: flex;
  justify-content: space-between;
  .business-item {
    margin-bottom: 20px;
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    width: 128px;
    height: 101px;
    background: url(../../static/assets/BusinessSituation/business_item_bg.png)
      no-repeat center bottom;
    text-align: center;
  }
}
</style>
